package service;

import java.util.Arrays;
import java.util.List;

import javax.jws.WebService;

import bean.Employee;
import bean.Flight;
import bean.FlightUpdate;
import bean.Journey;
import bean.Person;
import bean.ReservationDetails;
import bean.Traveler;
import dao.EmployeeDAO;
import dao.EmployeeDAOImpl;
import dao.FlightDAO;
import dao.FlightDAOImpl;
import dao.JourneyDAO;
import dao.JourneyDAOImpl;
import dao.PersonDAOImpl;
import dao.TravelerDAO;
import dao.TravelerDAOImpl;
import exception.DAOException;

@WebService
public class AirlineReservationService {
	
	
	
	/**
	 * 
	 * @return
	 * @throws DAOException 
	 */
	public Journey[] viewAllJourney() throws DAOException{
		
			JourneyDAO journeyDetailsDAO=new JourneyDAOImpl();
			List<Journey> alljourneydetails = journeyDetailsDAO.viewAllJourney();
			Journey arrJourney[] = new Journey[alljourneydetails.size()];
			return alljourneydetails.toArray(arrJourney);
		}
	
	public ReservationDetails[] viewAllReservation(long confirmationNumer) throws DAOException{
		
		JourneyDAO reservationDetailsDAO=new JourneyDAOImpl();
		List<ReservationDetails> allreservationdetails = reservationDetailsDAO.viewAllReservation(confirmationNumer);
		ReservationDetails arrReservation[] = new ReservationDetails[allreservationdetails.size()];
		return allreservationdetails.toArray(arrReservation);
		
		
		
	}
	
	public ReservationDetails[] viewAllPassengers(long flightId,String journeyDate) throws DAOException
	{
		JourneyDAO journeyDetailsDAO=new JourneyDAOImpl();
		List<ReservationDetails> allreservationdetails = journeyDetailsDAO.viewAllPassengers(flightId,journeyDate);
		ReservationDetails arrReservation[] = new ReservationDetails[allreservationdetails.size()];
		return allreservationdetails.toArray(arrReservation);
		
		
	}

	/**
	 * 
	 * @param source
	 * @param destination
	 * @param startDate
	 * @param endDate
	 * @param tripType
	 * @return
	 * This method will return all flights based on the paramters passed.
	 */
	public Flight[] searchFlights(String source, String destination, String startDate, String returnDate, String tripType, int numTravelers) throws DAOException{
		FlightDAO flightDAO = new FlightDAOImpl();
		List<Flight> flights = flightDAO.searchFlights(source, destination, startDate, returnDate, tripType, numTravelers);
		Flight arrFlights[] = new Flight[flights.size()];
		return flights.toArray(arrFlights);
		
	}
	
	
	/**
	 * 
	 * @return
	 * @throws DAOException 
	 */
	
	public Flight[] viewFlights() throws DAOException{
		FlightDAO flightDAO = new FlightDAOImpl();
		List<Flight> flights = flightDAO.viewFlights();
		Flight arrFlights[] = new Flight[flights.size()];
		return flights.toArray(arrFlights);
		
	}
	
	/**
	 * 
	 * @param flightId
	 * @return
	 * This method will make a reservation and returns an array of Journey objects with confirmation number
	 */
	
	public Journey[] reserve(int numTickets, Journey[] journey, long personId) throws DAOException{
		JourneyDAO journeyDAO = new JourneyDAOImpl();
		List<Journey> lstJourney = journeyDAO.reserve(numTickets, journey, personId);
		Journey arrJourney[] = new Journey[lstJourney.size()];
		return lstJourney.toArray(arrJourney);
	}
	/**
	 * 
	 * @param confirmationNumber
	 * @return
	 * This method will cancel a reservation
	 */
	public void cancelReservation(long confirmationNumber) throws DAOException{
		new JourneyDAOImpl().deleteReservation(confirmationNumber);
	}
	
	/**
	 * 
	 * @param flightNumber
	 * @return
	 * This method returns all the reservations for a particular flight id. 
	 * This is mainly used by airline personnel.
	 */
	public Journey[] viewReservationsForFlight(int flightId){
		return null;
	}
	
	
	
	/**
	 * 
	 * @return
	 */
	public Journey[] viewAllReservations(){
		return null;
	}
	
	
	
	/**
	 * 
	 * @param username
	 * @return
	 * This method returns the reservations made by a particular user
	 */
	public Journey[] viewReservationsForTraveller(long personId) throws DAOException{
		JourneyDAO journeyDAO = new JourneyDAOImpl();
		List<Journey> lstJourney = journeyDAO.viewReservationsForTraveller(personId);
		Journey arrJourney[] = new Journey[lstJourney.size()];
		return lstJourney.toArray(arrJourney);
		
	}
	
	/**
	 * 
	 * @param confirmationNumber
	 * @return
	 * This method gives reservation details for a particular confirmation number
	 */
	public Journey viewReservationDetails(long confirmationNumber){
		return null;
	}
	
	 /**
	  * 
	  * @param firstName
	  * @param lastName
	  * @param userName
	  * @param password
	  * @param addressLine1
	  * @param addressLine2
	  * @param city
	  * @param state
	  * @param dateOfBirth
	  * @param id
	  */
	public void createEmployee(String firstName, String lastName, String userName, String password, String addressLine1,
			String addressLine2, String city, String state, String dateOfBirth, long id, String type, String workDescription, String hireDate, String position, String role, long created_by, String created_on , long updated_by, String updated_on, long zip) throws DAOException{
		EmployeeDAO employeeDAO = new EmployeeDAOImpl();
		employeeDAO.createEmployee(firstName, lastName, userName, password, addressLine1, addressLine2, city, state, dateOfBirth, id, type, workDescription, hireDate, position, role, created_by, created_on, updated_by, updated_on, zip);
		
	}
	
	/**
	 * 
	 * @param firstName
	 * @param lastName
	 * @param userName
	 * @param password
	 * @param addressLine1
	 * @param addressLine2
	 * @param city
	 * @param state
	 * @param dateOfBirth
	 * @param id
	 * @param passportNumber
	 * @param nationality
	 */
	public void createCustomer(String firstName, String lastName, String userName, String password, String addressLine1,
			String addressLine2, String city, String state, String dateOfBirth, long id, String type, String passportNumber, String nationality, long zip) throws DAOException{
		new TravelerDAOImpl().createTraveler(firstName, lastName, userName, password, addressLine1, addressLine2, city, state, dateOfBirth, id, type, passportNumber, nationality, 1, null, 1, null, zip);
	}
	
	/**
	 * 
	 * @param firstName
	 * @param lastName
	 * @param userName
	 * @param password
	 * @param addressLine1
	 * @param addressLine2
	 * @param city
	 * @param state
	 * @param dateOfBirth
	 * @param id
	 * @param passportNumber
	 * @param nationality
	 */
	public void editCustomer(String firstName, String lastName, String userName, String password, String addressLine1,
			String addressLine2, String city, String state, String dateOfBirth, long id, String passportNumber, String nationality, long zip) throws DAOException{
		new TravelerDAOImpl().updateTraveler(firstName, lastName, userName, password, addressLine1, addressLine2, 
				city, state, dateOfBirth, id, null, passportNumber, nationality, 1, null, 1, null, zip);
		
	}
	
	/**
	 * 
	 * @param firstName
	 * @param lastName
	 * @param userName
	 * @param password
	 * @param addressLine1
	 * @param addressLine2
	 * @param city
	 * @param state
	 * @param dateOfBirth
	 * @param id
	 * @param workDescription
	 * @param hireDate
	 * @param position
	 * @param role
	 */
	public void editEmployee(String firstName, String lastName, String userName, String password, String addressLine1,
			String addressLine2, String city, String state, String dateOfBirth, long id, String type,String workDescription, String hireDate, String position, String role, long created_by, String created_on , long updated_by, String updated_on, long zip)throws DAOException{
		EmployeeDAO employeeDAO = new EmployeeDAOImpl();	
		employeeDAO.updateEmployee(firstName, lastName, userName, password, addressLine1, addressLine2, city, state, dateOfBirth, id, type, workDescription, hireDate, position, role, created_by, created_on, updated_by, updated_on, zip);
	}
	
	/**
	 * 
	 * @param id
	 */
	public void deletePerson(long id){
		
	}
	
	public void deleteEmployee(long id) throws DAOException{
		EmployeeDAO employeeDAO = new EmployeeDAOImpl();
		employeeDAO.deleteEmployee(id);
	}
	
	public void deleteCustomer(long id){
		new TravelerDAOImpl().deleteTraveler(id);
	}
	
	/**
	 * 
	 * @return
	 */	
	public Traveler[] listCustomers()throws DAOException{
		TravelerDAO travelerDAO = new TravelerDAOImpl();
		List<Traveler> travelers = travelerDAO.viewTravelers();
		Traveler arrTraveler[] = new Traveler[travelers.size()];
		return travelers.toArray(arrTraveler);
	}
	
	public Traveler[] viewTravelerByFlight(int flightId) throws DAOException{
		TravelerDAO travelerDAO = new TravelerDAOImpl();
		List<Traveler> travelers = travelerDAO.viewTravelerByFlight(flightId);
		Traveler arrTraveler[] = new Traveler[travelers.size()];
		return travelers.toArray(arrTraveler);
	}
	
	/**
	 * 
	 * @return
	 */
	public Employee[] listEmployees()throws DAOException{
		EmployeeDAO empDAO = new EmployeeDAOImpl();
		List<Employee> empl = empDAO.viewEmployees();
		Employee arrEmpl[] = new Employee[empl.size()];
		return empl.toArray(arrEmpl);
	}
	
	
	/**
	 * 
	 * @param confirmationNumber
	 * @return
	 */
	public Journey issueTicket(long confirmationNumber) throws DAOException{
			
		return new JourneyDAOImpl().issueTicket(confirmationNumber);
	}
	
	/**
	 * 
	 * @param firstName
	 * @param lastName
	 * @param id
	 * @param workDescription
	 * @return
	 */
	
	public Employee[] searchEmployees(String firstName, String lastName, long id, String workDescription){
		return null;
	}
	
	
	public Flight[] getFlightDetails(long[] flightIds){
		FlightDAO flightDAO = new FlightDAOImpl();
		String strFlightIds = "";
		for(long id : flightIds){
			strFlightIds += id+",";
		}
		System.out.println("strFlightIds "+ strFlightIds);
		List<Flight> flights = flightDAO.getFlightDetails(strFlightIds.substring(0,strFlightIds.length()-1));
		Flight arrFlights[] = new Flight[flights.size()];
		return flights.toArray(arrFlights);
		
		
	}
	
	public Person authenticateUser(String username, String password) throws DAOException{
		
		return new PersonDAOImpl().authenticateUser(username, password);
	}
	
	public boolean checkUser(String username) throws DAOException{
		return new PersonDAOImpl().checkUser(username);
	}
	
	public void deleteReservation (long confirmationNumber) throws DAOException{
		new JourneyDAOImpl().deleteReservation(confirmationNumber);
	}
	
	public Employee getEmployeeDetails(long id) throws DAOException {
	 return new EmployeeDAOImpl().getEmployeeDetails(id) ;
	}
	
	public Traveler getTravelerDetails(long id) throws DAOException {
		 return new TravelerDAOImpl().getTravelerDetails(id) ;
	}
	
	
	public Journey[] viewFlightsByDate()  throws DAOException{
		JourneyDAO journeyDAO = new JourneyDAOImpl();
		List<Journey> lstJourney = journeyDAO.viewJourneyByDate();
		Journey arrJourney[] = new Journey[lstJourney.size()];
		return lstJourney.toArray(arrJourney);
	}

	
	public void postUpdate(long flightId, String journeyDate, String message) throws DAOException{
		new JourneyDAOImpl().postFlightUpdate(flightId, journeyDate, message);
	}
	
	
	public FlightUpdate[] retrieveFlightUpdate(long personId){
		FlightDAO flightDAO = new FlightDAOImpl();
		List<FlightUpdate> lstFlightUpates = flightDAO.retrieveFlightUpdates(personId);
		FlightUpdate arrFlightUpdate[] = new FlightUpdate[lstFlightUpates.size()];
		return lstFlightUpates.toArray(arrFlightUpdate);
	}
	
	public Flight changeFlightInfo(String flightNumber,String airlineName,String source,String destination,int numSeats,String pilotName,String intermediate,String intermediateArrivalTime,String intermediateDepartureTime,String timeOfFlight,double ticketPrice,String status,long flightId) throws DAOException
	{
		FlightDAO flightDAO = new FlightDAOImpl();
		Flight flight=flightDAO.changeFlightInfo(flightNumber, airlineName, source, destination, numSeats, pilotName, intermediate, intermediateArrivalTime, intermediateDepartureTime, timeOfFlight, ticketPrice, status, flightId);
		
		return flight;
		
	}
	
	public Employee[] searchEmployeebyAttr(String[] tbl, String[] paramName, String[] ParamValue)throws DAOException{
		EmployeeDAO empDAO = new EmployeeDAOImpl();
		List<String> lstTbl = Arrays.asList(tbl);
		List<String> lstParamName = Arrays.asList(paramName);
		List<String> lstParamValue = Arrays.asList(ParamValue);
		
		List<Employee> empl = empDAO.searchEmployeeByAttr(lstTbl, lstParamName, lstParamValue );
		Employee arrEmpl[] = new Employee[empl.size()];
		return empl.toArray(arrEmpl);
	}
	
	
	public String[] retreiveCities() throws DAOException{
		List<String> lstCities = new FlightDAOImpl().retrieveCities();
		String arrCities[] = new String[lstCities.size()];
		return lstCities.toArray(arrCities);
	}
}
